﻿using Common.BaseLibrary.Authorization;
using DepartmentWork.Migrator.Data;
using DepartmentWork.Model;
using DepartmentWork.Platform.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DepartmentWork.Platform.Areas.QueryArea.Controllers
{
    public class DeskWorkSelcetController :  BaseController
    {
        //
        // GET: /QueryArea/DeskWorkSelcet/


        /// <summary>
        /// 科室工作查询界面
        /// </summary>
        /// <returns></returns>
        public ActionResult SelcetDeskWork()
        {
            if (!string.IsNullOrEmpty(CurrentUser.UserID))
            {

                #region 获取该团队空间下的全部人员
                Guid teamSpaceId = userTeamsapceRepository.GetModel(r => r.USER_ID == new Guid(CurrentUser.UserID)).FirstOrDefault().TEAMSPACE_ID;
                List<UserInfoViewModel> userList = new List<UserInfoViewModel>();
                List<USER_TEAMSAPCE> userTeams = userTeamsapceRepository.GetModel(r => r.TEAMSPACE_ID == teamSpaceId).ToList();
                userList.AddRange((from item in userTeams
                                   select new UserInfoViewModel()
                                   {
                                       UserId = item.USER_ID
                                   }).ToList());
                for (int i = 0; i < userList.Count(); i++)
                {
                    var id = userList[i].UserId;
                    userList[i].UserName =
                        userRepository.GetModel(r => r.USER_ID == id).FirstOrDefault().DISPLAY_NAME;
                }
                TempData["UserList"] = userList;

                #endregion

            }


 

            return View();
        }




        /// <summary>
        /// 全员请假查询ajax
        /// </summary>
        /// <param name="NameId"></param>
        /// <param name="BTime"></param>
        /// <param name="DTime"></param>
        /// <returns></returns>
        public JsonResult SelcetS(string NameN,string NameId,string NameTime, DateTime BTime, DateTime DTime)
        {


            //r.DESKWORK_DEADINE.Subtract(DateTime.Now).Days < 0
            string str = NameN.Trim() ;
            List<ProjectDeskWorkViewModel> list = new List<ProjectDeskWorkViewModel>();
            List<PROJECT_DESK_WORK> model = new List<PROJECT_DESK_WORK>();
            if (str != "")//如果有关键字
            {
                #region 全部负责人
                if (NameId == "0") //负责人：全部 
                {
                    if (NameTime == "0")//是否超时：全部     只按照工作关键字和时间段查出所有
                    {
                        model = projectdeskworkRepository.GetModel(r => r.DESKWORK_NAME.Contains(NameN) && r.DESKWORK_CREATETIME >= BTime && r.DESKWORK_CREATETIME <= DTime).OrderByDescending(r => r.DESKWORK_CREATETIME).ToList();
                    }
                    if (NameTime == "1")//是否超时：超时      按照关键字和超时，时间段去查询
                    {
                        model = projectdeskworkRepository.GetModel(r => r.DESKWORK_NAME.Contains(NameN) && r.DESKWORK_CREATETIME >= BTime && r.DESKWORK_CREATETIME <= DTime && r.DESKWORK_OVERTIME > r.DESKWORK_DEADINE).OrderByDescending(r => r.DESKWORK_CREATETIME).ToList();
                    }
                    if (NameTime == "2")// 是否超时：未超时     按照关键字和为超时，时间段去查询
                    {
                        model = projectdeskworkRepository.GetModel(r => r.DESKWORK_NAME.Contains(NameN) && r.DESKWORK_CREATETIME >= BTime && r.DESKWORK_CREATETIME <= DTime && r.DESKWORK_OVERTIME <= r.DESKWORK_DEADINE).OrderByDescending(r => r.DESKWORK_CREATETIME).ToList();
                    }
                }
                #endregion
                #region  选择了指定的负责人
                else //选择了指定的负责人
                {
                    Guid NameID = new Guid(NameId);//负责人ID
                    if (NameTime == "0")
                    {
                        model = projectdeskworkRepository.GetModel(r => r.DESKWORK_NAME.Contains(NameN) && r.DESKWORK_PRINCIPAL_ID == NameID && r.DESKWORK_CREATETIME >= BTime && r.DESKWORK_CREATETIME <= DTime).OrderByDescending(r => r.DESKWORK_CREATETIME).ToList();
                    }
                    if (NameTime == "1")
                    {
                        model = projectdeskworkRepository.GetModel(r => r.DESKWORK_NAME.Contains(NameN) && r.DESKWORK_PRINCIPAL_ID == NameID && r.DESKWORK_CREATETIME >= BTime && r.DESKWORK_CREATETIME <= DTime && r.DESKWORK_OVERTIME > r.DESKWORK_DEADINE).OrderByDescending(r => r.DESKWORK_CREATETIME).ToList();
                    }
                    if (NameTime == "2")
                    {
                        model = projectdeskworkRepository.GetModel(r => r.DESKWORK_NAME.Contains(NameN) && r.DESKWORK_PRINCIPAL_ID == NameID && r.DESKWORK_CREATETIME >= BTime && r.DESKWORK_CREATETIME <= DTime && r.DESKWORK_OVERTIME <= r.DESKWORK_DEADINE).OrderByDescending(r => r.DESKWORK_CREATETIME).ToList();
                    }
                }
                #endregion
            }
            else // 如果没有关键字
            {
                #region 全部负责人
                if (NameId == "0") //负责人：全部 
                {
                    if (NameTime == "0")//是否超时：全部     只按照工作关键字和时间段查出所有
                    {
                        model = projectdeskworkRepository.GetModel(r => r.DESKWORK_CREATETIME >= BTime && r.DESKWORK_CREATETIME <= DTime).OrderByDescending(r => r.DESKWORK_CREATETIME).ToList();
                    }
                    if (NameTime == "1")//是否超时：超时      按照关键字和超时，时间段去查询
                    {
                        model = projectdeskworkRepository.GetModel(r => r.DESKWORK_CREATETIME >= BTime && r.DESKWORK_CREATETIME <= DTime && r.DESKWORK_OVERTIME > r.DESKWORK_DEADINE).OrderByDescending(r => r.DESKWORK_CREATETIME).ToList();
                    }
                    if (NameTime == "2")// 是否超时：未超时     按照关键字和为超时，时间段去查询
                    {
                        model = projectdeskworkRepository.GetModel(r => r.DESKWORK_CREATETIME >= BTime && r.DESKWORK_CREATETIME <= DTime && r.DESKWORK_OVERTIME <= r.DESKWORK_DEADINE).OrderByDescending(r => r.DESKWORK_CREATETIME).ToList();
                    }
                }
                #endregion
                #region  选择了指定的负责人
                else //选择了指定的负责人
                {
                    Guid NameID = new Guid(NameId);//负责人ID
                    if (NameTime == "0")
                    {
                        model = projectdeskworkRepository.GetModel(r => r.DESKWORK_PRINCIPAL_ID == NameID && r.DESKWORK_CREATETIME >= BTime && r.DESKWORK_CREATETIME <= DTime).OrderByDescending(r => r.DESKWORK_CREATETIME).ToList();
                    }
                    if (NameTime == "1")
                    {
                        model = projectdeskworkRepository.GetModel(r => r.DESKWORK_PRINCIPAL_ID == NameID && r.DESKWORK_CREATETIME >= BTime && r.DESKWORK_CREATETIME <= DTime && r.DESKWORK_OVERTIME > r.DESKWORK_DEADINE).OrderByDescending(r => r.DESKWORK_CREATETIME).ToList();
                    }
                    if (NameTime == "2")
                    {
                        model = projectdeskworkRepository.GetModel(r => r.DESKWORK_PRINCIPAL_ID == NameID && r.DESKWORK_CREATETIME >= BTime && r.DESKWORK_CREATETIME <= DTime && r.DESKWORK_OVERTIME <= r.DESKWORK_DEADINE).OrderByDescending(r => r.DESKWORK_CREATETIME).ToList();
                    }
                }
                #endregion
            }

            if (model.Count > 0)
            {
                foreach (PROJECT_DESK_WORK x in model)
                {
                    ProjectDeskWorkViewModel pl = new ProjectDeskWorkViewModel();

                    //string name = userRepository.GetModel(r => r.USER_ID == x.).FirstOrDefault().DISPLAY_NAME;
                    pl.DESKWORK_CREATETIME = x.DESKWORK_CREATETIME;
                    pl.DESKWORK_CREATETIME_SELCET = x.DESKWORK_CREATETIME.ToString("yyyy-MM-dd");
                    pl.DESKWORK_DEADINE = x.DESKWORK_DEADINE;
                    pl.DESKWORK_DEADINE_SELCET = x.DESKWORK_DEADINE.ToString("yyyy-MM-dd");
                    pl.DESKWORK_DESCRIBE = x.DESKWORK_DESCRIBE;
                    if (x.DESKWORK_STATUS == "1")
                    {
                        pl.DESKWORK_STATUS = "完成";
                    }
                    if (x.DESKWORK_STATUS == "0")
                    {
                        pl.DESKWORK_STATUS = "未完成";
                    }
                    pl.DESKWORK_EDITOR = x.DESKWORK_EDITOR;
                    pl.DESKWORK_FOUNDER = x.DESKWORK_FOUNDER;
                    pl.DESKWORK_ID = x.DESKWORK_ID;
                    pl.DESKWORK_NAME = x.DESKWORK_NAME;
                    pl.DESKWORK_NO = x.DESKWORK_NO;
                    pl.DESKWORK_OVERTIME = x.DESKWORK_OVERTIME;
                    if (x.DESKWORK_STATUS == "1")
                    {
                        pl.DESKWORK_OVERTIME_SELCET = x.DESKWORK_OVERTIME.ToString("yyyy-MM-dd");
                    }
                    else
                    {
                        pl.DESKWORK_OVERTIME_SELCET = "";
                    }
                    pl.DESKWORK_PRINCIPAL = x.DESKWORK_PRINCIPAL;
                    list.Add(pl);
                }
            }

            return Json(list);
        }





    }
}
